<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>首页</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <link rel="stylesheet" href="https://lib.sinaapp.com/js/bootstrap/4.2.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
    <link rel="stylesheet" href="css/scroller.css">
    <link rel="stylesheet" href="css/my.css">
</head>
<body>
<div class="page login-page">
    <div class="container d-flex align-items-center">
        <div class="form-holder has-shadow">
            <div class="row">
                <!-- Logo & Information Panel-->
                <div class="col-lg-6">
                    <div class="info d-flex align-items-center">
                        <div class="content">
                            <h1>Let's chat😀一起聊天吧</h1>
<!--                            <div class="logo">-->
<!--                                <h1>小克的空间</h1>-->
<!--                            </div>-->
<!--                            <p>我的信息</p>-->
                            <div class="form-group">
                                <div class="infinity-scroll">
                                    <div class="main-content" id="friendTable">
<!--                                        <div class="item first-item" >1fr</div>-->
<!--                                            -->
<!--                                        <div class="item last-item">30</div>-->
<!--                                        <div class="item" data-items-index="1">2</div>-->
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <!-- Form Panel    -->
                <div class="col-lg-6 bg-white">
                    <div class="form d-flex align-items-center">
                        <div class="content">
                            <h1>我的空间</h1>
                            <form method="post" action="login.html" class="form-validate" id="loginFrom">
                                <div class="form-group">
                                    <img id="profile" src="#" width="100" height="100" style="border-radius: 30px" alt="暂无头像">
                                    <input type="file" id="input_file" name="input_file" class="input-material">
                                    <button id="save_profile" type="button" class="btn btn-primary">保存头像</button>
                                </div>
                                <div class="form-group">
                                    <label for="name">昵称</label>
                                    <input id="name" class="input-material"/>
                                    <button id="update_name" type="button" class="btn btn-primary">修改</button>
                                    <button id="save_name" type="button" class="btn btn-primary">保存</button>
                                </div>
                                <div class="form-group">
                                    <label for="time">注册时间</label>
                                    <input id="time" disabled="disabled" class="input-material"/>
                                </div>
                                <div style="margin-top: -40px;">

                                </div>
                            </form>
                            <br/><br/>
                            <a id="login_out" href="login.html" class="signup">&nbsp;注销</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- JavaScript files-->
<script src="https://s1.pstatp.com/cdn/expire-1-M/jquery/1.10.2/jquery.min.js"></script>
<script src="https://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<!-- Main File-->

</body>
</html>

<script src="js/friendTable.js" type="text/javascript"></script>
<script src="js/jquery-3.6.0.js" type="text/javascript"></script>
<script>
    let email = sessionStorage.getItem('email')
    let data = {}
    data.email = email;
    data.action = "getInfo";
    $.post("./Login",data,function(json) {
        console.log(json);
        json = JSON.parse(json);
        if(json.result === 1 || json.result === "1"){
            document.getElementById('profile').src = "/chat"+json.url;
            sessionStorage.setItem('url', "/chat"+json.url);
            document.getElementById('name').value = json.name;
            document.getElementById('time').value = json.time;
            document.getElementById('name').disabled = "disabled";
        }
        else {
            alert(json.reason);
        }
    })
</script>

<script>
    SaveName=function (){
        let email = sessionStorage.getItem('email');
        let name = $('#name').val();
        if(name===""){
            alert("昵称不能为空!");
            return;
        }
        if(name.indexOf(" ")!==-1){
            alert("昵称不能包含空格!");
            return;
        }
        let data = {}
        data.email = email;
        data.name = name;
        data.action = "update_name";
        $.post("./Login",data,function(json) {
            console.log(json);
            json = JSON.parse(json);
            if(json.result === 1 || json.result === "1"){
                alert("保存成功!")
                window.location.reload();
            }
            else {
                alert(json.reason);
            }
        })
    }


    document.getElementById('save_name').addEventListener('click',SaveName);
    // document.getElementById('input_file').onchange.previewProfile();
    document.getElementById('update_name').addEventListener('click',function (){
        document.getElementById('name').disabled = "";
    });
</script>

<script>
    let UploadFile=function(){
        // 初始化文件上传的界面
        let filename = $("#input_file").val();
        if(filename===""){
            alert("请选择图片！仅支持png,jpg,jpeg格式的图片");
            return;
        }
        let formData = new FormData();
        formData.append("files", $("#input_file")[0].files[0]);
        formData.append("email", sessionStorage.getItem('email'));

        $.ajax({
            type: "POST",
            url: "./uploadProfile?action=upload_profile&email="+sessionStorage.getItem('email'),
            enctype: 'multipart/form-data',
            data: formData,
            dataType:"json",                //这一项必不可少
            contentType : false,            //这一项必不可少
            processData : false,            //这一项必不可少
            success: function (json) {
                console.log(JSON.stringify(json));
                if(json.result !== 1){
                    alert(json.reason);
                }
                else {
                    window.location.reload();
                }
            }
        });
    };

    document.getElementById('login_out').addEventListener('click',function (){sessionStorage.clear();})
    document.getElementById('save_profile').addEventListener('click',UploadFile);
</script>